package PreparedStatement;

import entity.Sys;
import util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Jason
 * @PACKAGE_NAME AddDeleteCheckAndCorrect
 * @Description 增加数据
 * @date 2023-12-06 9:29
 */
public class AddTest {
    public static int add(Sys sys) throws SQLException, ClassNotFoundException {
        String sql = "insert into sys(username,password) values(?,?)";
        DbUtil dbUtil = new DbUtil();
        Connection conn = dbUtil.getConnection();
        /*预编译*/
        PreparedStatement pst = conn.prepareStatement(sql);
        /*填充值*/
        pst.setString(1,sys.getUsername());
        pst.setString(2,sys.getPassword());
        int execute = pst.executeUpdate();
        System.out.println("操作了"+execute+"条数据");
        /*关闭数据库*/
        dbUtil.closeStatement(pst);
        dbUtil.closeConnection(conn);
        return execute;
    }
    public static void main(String[] args) {
        try {
            Sys s = new Sys("a","b");
            int add = AddTest.add(s);
            if(add != 0){
                System.out.println("新增成功");
            }
            else{
                System.out.println("新增失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
